package com.sfh.gd.service.rule.impl;

import com.sfh.gd.domain.dto.GameRuleDTO;
import com.sfh.gd.domain.entity.GamePKDO;
import com.sfh.gd.service.rule.RuleFactory;

import java.time.LocalDateTime;

/**
 * 积分晋级赛 - 积分计算规则
 */
public class PointsComputeA implements RuleFactory.Compute {

    GameRuleDTO gameRuleDTO;

    public PointsComputeA(GameRuleDTO gameRuleDTO) {
        this.gameRuleDTO = gameRuleDTO;
    }
    /**
     * 积分晋级赛 - 积分计算规则-弃权
     * @param winTeamId 胜利方ID
     * @param gamePKDO 比赛对象
     */
    @Override
    public GamePKDO computeTeamAbandon(Long winTeamId, GamePKDO gamePKDO ) {

        this.computeTeam(winTeamId, gamePKDO,gameRuleDTO);
        //比赛用时
        gamePKDO.setGameTime(gameRuleDTO.getAbstainTime());

        return gamePKDO;

    }

    /**
     * 积分晋级赛 - 积分计算规则-正常
     * @param winTeamId 胜利方ID
     * @param gamePKDO 比赛对象
     */
    @Override
    public GamePKDO computeTeamNormal(Long winTeamId, GamePKDO gamePKDO ) {

        this.computeTeam(winTeamId, gamePKDO,gameRuleDTO);
        //比赛用时(分钟)= 当前时间-PK开始时间
        long gameTime = java.time.Duration.between(gamePKDO.getCreateTime(), LocalDateTime.now()).toMinutes();
        gamePKDO.setGameTime(gameTime);

        return gamePKDO;
    }

    private void computeTeam(Long winTeamId, GamePKDO gamePKDO, GameRuleDTO gameRuleDTO ) {
        long score = 0;
        if (gamePKDO.getTeamAId() == winTeamId) {
            //B组负方
            score = Math.max(gamePKDO.getTeamBPoints() * gameRuleDTO.getPointsRatio(), 10);
            //A胜方 ID
            gamePKDO.setWinTeamId(gamePKDO.getTeamAId());
            //负方减分 积分小于0 淘汰，轮次+1 状态 1 正常 -1 淘汰 2禁赛 3比赛中 4等待中 5已晋级
            gamePKDO.getTeamB().subScoreValue(score).addRoundCount().setState(gamePKDO.getTeamB().getScoreValue() <= 0 ? -1 : 4);
            //胜加分 轮次+1 是否晋级
            gamePKDO.getTeamA().addScoreValue(score).addRoundCount().setState(gamePKDO.getTeamA().getScoreValue() >= gameRuleDTO.getPromotePoints() ? 5 : 4);

        } else {

            //A组负方 结算分=max(负方分*得分比例,10)
            score = Math.max(gamePKDO.getTeamAPoints() * gameRuleDTO.getPointsRatio(), 10);
            //胜方 ID
            gamePKDO.setWinTeamId(gamePKDO.getTeamBId());
            //负方减分 积分小于0 淘汰，轮次+1 状态 1 正常 -1 淘汰 2禁赛 3比赛中 4等待中 5已晋级
            gamePKDO.getTeamA().subScoreValue(score).addRoundCount().setState(gamePKDO.getTeamA().getScoreValue() <= 0 ? -1 : 4);
            //胜加分 轮次+1 是否晋级
            gamePKDO.getTeamB().addScoreValue(score).addRoundCount().setState(gamePKDO.getTeamA().getScoreValue() >= gameRuleDTO.getPromotePoints() ? 5 : 4);
        }
        if (gamePKDO.getTeamA().getState() == 4){
            //更新兼容PK等待中时间
            gamePKDO.getTeamA().setWaitingTime(LocalDateTime.now());
        }
        if (gamePKDO.getTeamB().getState() == 4){
            //更新兼容PK等待中时间
            gamePKDO.getTeamB().setWaitingTime(LocalDateTime.now());
        }
        //结算积分值
        gamePKDO.setPoints(score);
        //状态 已结束
        gamePKDO.setState(2);
    }
}
